/**
 * Created by Administrator on 2017/8/23 0023.
 */

var container;
var content;
var closer;
/**
 * Create an overlay to anchor the popup to the map.
 */
//add the overlay for displaying cell msg

var overlay;



var selectClick = new ol.interaction.Select({
    condition: ol.events.condition.click
//                condition: ol.events.condition.pointerMove
});

var addCellSelect = false;

function addCellSelectFun(){
    //var popup = new ol.Overlay({
    //    element: document.getElementById('popup')
    //});
    container = document.getElementById('popup');
    content = document.getElementById('popup-content');
    closer = document.getElementById('popup-closer');
    /**
     * Add a click handler to hide the popup.
     * @return {boolean} Don't follow the href.
     */
    closer.onclick = function() {
        overlay.setPosition(undefined);
        closer.blur();
        return false;
    };
    if(map || overlay ){
        overlay = new ol.Overlay(/** @type {olx.OverlayOptions} */ ({
            element: container,
            autoPan: false,
            autoPanAnimation: {
                duration: 250
            }
        }));
        map.addOverlay(overlay);
    }else{
        alert('there is no map at all!');
    }
    if (!addCellSelect) {
        //add select function
        map.addInteraction(selectClick);
        selectClick.on('select', function(e) {
            var features = e.target.getFeatures();
            if(features.getLength()<1){
                return
            }
            var feature = features.pop();
            var cell_name = feature.R.cell_name;
            var coordinate = feature.getGeometry().getCoordinates()[0][0];
            var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(
                coordinate, 'EPSG:3857', 'EPSG:4326'));

            content.innerHTML = '<p>当前小区名：'+cell_name+',经纬度为：</p><code>' + hdms +
                '</code>';
            overlay.setPosition(coordinate);

        });
        alert('cellSelect add success!');
    }else{
        map.removeInteraction(selectClick);
        alert('cellSelect has been remove!')
    }
    addCellSelect = !addCellSelect;
}
